<?php

/**
 * Zend Framework
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Gdata
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */

/**
 * @see Zend_Gdata_Extension
 */
require_once 'Zend/Gdata/Extension.php';

/**
 * Data model class to represent money
 *
 * @category   Zend
 * @package    Zend_Gdata
 * @copyright  Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Zend_Gdata_Finance_Extension_Money extends Zend_Gdata_Extension
{

	protected $_rootElement = 'money';
	/**
	 * Amount of money
	 *
	 * @var string
	 */
	protected $_amount = null;
	/**
	 * ISO Currency Code
	 *
	 * @var string
	 */
	protected $_currencyCode = null;

	/**
	 * Constructs a new Zend_Gdata_Finance_Extension_Money object.
	 * @param string $amount (optional) Amount of money.
	 * @param string $currencyCode (optional) ISO4217 currency code.(http://www.iso.org/iso/en/prods-services/popstds/currencycodeslist.html)
	 */
	public function __construct($amount = null, $currencyCode = null)
	{
		parent::__construct();
		if ($amount != null )
		$this->setAmount($amount);
		if ($currencyCode != null)
		$this->setCurrencyCode($currencyCode);
	}

	/**
	 * Retrieves a DOMElement which corresponds to this element and all
	 * child properties.  This is used to build an entry back into a DOM
	 * and eventually XML text for sending to the server upon updates, or
	 * for application storage/persistence.
	 *
	 * @param DOMDocument $doc The DOMDocument used to construct DOMElements
	 * @return DOMElement The DOMElement representing this element and all
	 * child properties.
	 */
	public function getDOM($doc = null)
	{
		$element = parent::getDOM($doc);
		if ($this->_amount != null) {
			$element->setAttribute('amount', $this->_amount);
		}
		if ($this->_currencyCode != null) {
			$element->setAttribute('currencyCode', $this->_currencyCode);
		}
		return $element;
	}

	/**
	 * Given a DOMNode representing an attribute, tries to map the data into
	 * instance members.  If no mapping is defined, the name and value are
	 * stored in an array.
	 *
	 * @param DOMNode $attribute The DOMNode attribute needed to be handled
	 */
	protected function takeAttributeFromDOM($attribute)
	{
		switch ($attribute->localName) {
			case 'amount':
				$this->_amount = $attribute->nodeValue;
				break;
			case 'currencyCode':
				$this->_currencyCode = $attribute->nodeValue;
				break;
			default:
				parent::takeAttributeFromDOM($attribute);
		}
	}

	/**
	 * Get the value for this element's Amount attribute.
	 *
	 * @return string The requested attribute.
	 */
	public function getAmount()
	{
		return $this->_amount;
	}

	/**
	 * Set the value for this element's Amount attribute.
	 *
	 * @param string $value The desired value for this attribute.
	 * @return Zend_Gdata_Finance_Extension_Money The element being modified.
	 */
	public function setAmount($value)
	{
		if ( is_numeric($value) )
		{
			$this->_amount = $value;
			return $this;
		}
		else
		{
			$exception = new Exception("Value is not numeric ($value)");
			throw $exception;
		}
	}

	/**
	 * Get the value for this element's CurrencyCode  attribute.
	 *
	 * @return string The requested attribute.
	 */
	public function getCurrencyCode()
	{
		return $this->_currencyCode;
	}

	/**
	 * Set the value for this element's CurrencyCode  attribute.
	 *
	 * @param string $value The desired value for this attribute.
	 * @return Zend_Gdata_Finance_Extension_Money The element being modified.
	 */
	public function setCurrencyCode($value)
	{
		if (is_string($value) && strlen($value) == 3)
		{
			$this->_currencyCode = $value;
			return $this;
		}
		else
		{
			$exception = new Exception("Value is not valid currency ($value)");
			throw $exception;
		}
	}
}
